
var student = require('./student');
var result="";

var mongodb = require('mongodb');

var mongodbServer = new mongodb.Server('localhost', 27017, { auto_reconnect: true, poolSize: 10 });
var db = new mongodb.Db('mydb', mongodbServer);

/* open db */
db.open(function() {
    /* Select 'contact' collection */
    db.collection('contact', function(err, collection) {
        /* Insert a data */
        collection.insert({
            name: 'zhangyue',
            email: 'zhangyue@gmail.com',
            tel: [
                '13167055985',
                '15488794562'
            ]
        }, function(err, data) {
            if (data) {
                console.log('Successfully Insert');
            } else {
                console.log('Failed to Insert');
            }
        });

        /* Querying */
        collection.findOne({ "name": 'zhangyue' }, function(err, data) {
            console.log('data:'+data._id);
            /* Found this People */
            if (data) {
                console.log('Name: ' + data.name + ', email: ' + data.email);
                result='Name: ' + data.name + ', email: ' + data.email;
            } else {
                console.log('Cannot found');
            }
        });
    });
});



var fs = require('fs'); 
var http = require('http');
var server = http.createServer(function (req, res) {

//res.writeHead(200, {'Content-Type': 'text/plain'});
// res.end('Hello World\ndata from nodejs and mogodb\n'+result);

fs.readFile('./index.html', 'utf-8',function (err, data) {//读取内容 
    if (err) throw err; 
    res.writeHead(200, {"Content-Type": "text/html"});//注意这里 
    res.write(data); 
    res.end('Hello World\ndata from nodejs and mogodb\n'+result); 
}); 


});
server.listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
console.log(student.student('test'));



/*var http = require("http"); 
var fs = require('fs'); 
exports.start = function(){ 
    http.createServer(function(request, response) { 
fs.readFile('./index.html', 'utf-8',function (err, data) {//读取内容 
    if (err) throw err; 
response.writeHead(200, {"Content-Type": "text/html"});//注意这里 
response.write(data); 
response.end(); 
}); 
}).listen(8888); 
    console.log("server start..."); 
} */

